package cn.odore.org.service.impl;

import cn.odore.basic.service.impl.BasicServiceImpl;
import cn.odore.basic.util.PageList;
import cn.odore.org.domain.Department;
import cn.odore.org.mapper.DepartmentMapper;
import cn.odore.org.query.DepartmentQuery;
import cn.odore.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 部门service层
 */
@Service
public class DepartmentServiceImpl extends BasicServiceImpl<Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    /**
     * 添加
     * @param d
     */
    @Override
    @Transactional
    public void add(Department d) {
        departmentMapper.add(d);
        // 处理dirPath路径问题
        if(d.getParent() == null || d.getParent().getId() == null){// 没有选择上级部门的情况
            d.setDirPath("/" + d.getId());
        }else{// 选择了上级部门的情况
            Department pd = departmentMapper.loadById(d.getParent().getId());
            d.setDirPath(pd.getDirPath() + "/" + d.getId());
        }
        departmentMapper.update(d);

    }

    /**
     * 修改
     * @param d
     */
    @Override
    @Transactional
    public void update(Department d) {
        // 处理dirPath路径问题
        if(d.getParent() == null || d.getParent().getId() == null){// 没有选择上级部门的情况
            d.setDirPath("/" + d.getId());
        }else{// 选择了上级部门的情况
            Department pd = departmentMapper.loadById(d.getParent().getId());
            d.setDirPath(pd.getDirPath() + "/" + d.getId());
        }
        departmentMapper.update(d);
    }

    /**
     * 通过id查询一条数据
     * @param id
     * @return
     */
    @Override
    public Department getById(Long id) {
        return departmentMapper.loadById(id);
    }

    @Override
    public void patchDel(List<Long> ids) {
        departmentMapper.patchDel(ids);
    }

    @Override
    public List<Department> getDeptTree() {
        return departmentMapper.getDeptTree();
    }
}
